<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/1/16
 * Time: 17:23
 */

namespace app\api\controller;


use think\Db;

class Shop extends Base
{
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
    }
    
    /**
     * 商家列表
     * page  当前页码
     * shopIds  商家筛选  全部为-1   选择多个商家时用逗号将商家ID隔开
     * num  任务排序  0默认时间排序  2任务数量从大到小   1 任务数量从小到大
     * @return \think\response\Json
     */
    public function shopList(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $page = $params['page'] ? $params['page'] : 1;
        $shopIds = $params['shopIds'];//商家筛选  多个商家用 逗号  隔开
        $num = $params['num'];//任务排序
        $where = [
            's_status' => 2,//认证状态
            's_sta' => 1//商家状态  1 正常  2 冻结
        ];
        if($shopIds != -1){//-1全部
            $where['s_id'] = ['IN', $shopIds];
        }
        if($num == 0){//0默认时间排序   1从大到小  2从小到大
            $order = 's_time desc';
        }elseif($num == 1){
            $order = 's_task_num asc';
        }else{
            $order = 's_task_num desc';
        }
        //商家列表
        $shopList = Db::table('up_shops')
            ->field('s_id, s_name, s_thumb, s_task_num')
            ->where($where)
            ->page($page, 10)
            ->order($order)
            ->select();
        // 我的关注/取消关注
        if($shopList){
            foreach($shopList as $k => &$v){
                //是否有关注的记录
                $follow = Db::table('up_follow')
                    ->field('follow_status')
                    ->where('follow_user_id', $params['user_id'])
                    ->where('follow_s_id', $v['s_id'])
                    ->find();
                if($follow){//如果有记录
                    $v['follow'] = $follow['follow_status'];
                }else{
                    $v['follow'] = 2;
                }
            }
            return json(['status' => 200, 'msg' => $shopList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    /**
     * 商家列表
     * page  当前页码
     * search  关键字
     * @return \think\response\Json
     */
    public function shopSearchList(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $page = $params['page'] ? $params['page'] : 1;
        $where = [
            's_status' => 2,//认证状态
            's_sta' => 1//商家状态  1 正常  2 冻结
        ];
        if(isset($params['search']) && $params['search']){
            $where['s_name'] = array('like','%'.$params['search'].'%');
        }
        $order = 's_time desc';
        //商家列表
        $shopList = Db::table('up_shops')
            ->field('s_id, s_name, s_thumb, s_task_num')
            ->where($where)
            ->page($page, 10)
            ->order($order)
            ->select();
        // 我的关注/取消关注
        if($shopList){
            foreach($shopList as $k => &$v){
                //是否有关注的记录
                $follow = Db::table('up_follow')
                    ->field('follow_status')
                    ->where('follow_user_id', $params['user_id'])
                    ->where('follow_s_id', $v['s_id'])
                    ->find();
                if($follow){//如果有记录
                    $v['follow'] = $follow['follow_status'];
                }else{
                    $v['follow'] = 2;
                }
            }
            return json(['status' => 200, 'msg' => $shopList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 筛选公司 名称和id
     * type  1 显示5个  2 显示全部
     * @return \think\response\Json
     */
    public function chooseShopById(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        if($params['type'] == 1){//展示5条
            $limit = 5;
        }else{
            $limit = '';//展开所有
        }
        $shopName = Db::table('up_shops')
            ->field('s_id, s_name')
            ->where('s_status', 2)
            ->where('s_sta', 1)
            ->limit($limit)
            ->select();
        if($shopName){
            return json(['status' => 200, 'msg' => $shopName]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 关注/取消关注操作
     * s_id  商家ID
     * @return \think\response\Json
     */
    public function doFollow(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $follow = Db::table('up_follow')
            ->field('follow_id, follow_status')
            ->where('follow_user_id', $params['user_id'])
            ->where('follow_s_id', $params['s_id'])
            ->find();
        if($follow){//关注过
            if($follow['follow_status'] == 1){//处于关注状态,点击取消关注
                $update['unfollow_time'] = date('Y-m-d H:i:s');
                $update['follow_status'] = 2;
                $status = 2;
            }else{//处于未关注状态,点击添加关注
                $update['follow_time'] = date('Y-m-d H:i:s');
                $update['follow_status'] = 1;
                $status = 1;
            }
            $res1 = Db::table('up_follow')->where('follow_id', $follow['follow_id'])->update($update);
            
            if($res1){
                $result = 1;
            }else{
                $result = 2;
            }
        }else{
            $data = [
                'follow_user_id' => $params['user_id'],
                'follow_s_id' => $params['s_id'],
                'follow_time' => date('Y-m-d H:i:s'),
                'follow_status' => 1
            ];
            $res1 = Db::table('up_follow')->insert($data);
            $status = 1;
            if($res1){
                $result = 1;
            }else{
                $result = 2;
            }
        }
        if($result == 1){
            return json(['status' => 200, 'msg' => $status]);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 商家详情
     * s_id  商家ID
     * @return \think\response\Json
     */
    public function shopDetail(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $detail = Db::table('up_shops')
            ->field('s_id, s_name, s_image, s_jianjie, s_task_num, s_address, s_status, s_sta,s_address')
            ->where('s_id', $params['s_id'])
            ->find();
        if(!$detail){
            return json(['status' => 0, 'msg' => '商家不存在']);
        }
        //商户被冻结或状态不是正常
        if($detail['s_status'] != 2 || $detail['s_sta'] == 2){
            return json(['status' => 0, 'msg' => '商家异常']);
        }
        //图片转换成数组
        if(!empty($detail['s_image'])){
            $detail['s_image'] = explode(',', $detail['s_image']);
        }else{
            $detail['s_image'] = [];
        }
        //是否关注
        $follow = Db::table('up_follow')
            ->field('follow_status')
            ->where('follow_user_id', $params['user_id'])
            ->where('follow_s_id', $detail['s_id'])
            ->find();
        if($follow){//有过关注记录
            $detail['follow'] = $follow['follow_status'];
        }else{
            $detail['follow'] = 2;
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 商家发布的所有进行中的任务
     * s_id  商家ID
     * @return \think\response\Json
     */
    public function shopTask(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $page = $params['page'] ? $params['page'] : 1;
        $taskList = Db::table('up_task')
            ->field('task_id, task_image, task_price, task_num, task_get_num, task_type')
            ->where('task_s_id', $params['s_id'])
            ->where('task_status', 2)
            ->page($page, 10)
            ->order('task_time desc')
            ->select();
        if($taskList){
            foreach($taskList as $k => &$v){
                $v['residue_task'] = $v['task_num'] - $v['task_get_num'];
            }
            return json(['status' => 200, 'msg' => $taskList]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
}